package com.zhxk.ruida.ruidaweb;

import android.annotation.SuppressLint;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zhxk.ruida.ruidaweb.view.FoundWebView;

public class MainActivity extends AppCompatActivity {
    private FoundWebView mainWeb;
    private WebSettings webSettings;
    public static final String EDU_PLATFORM_URL = "http://m.zjrdjr.com/";

    private SwipeRefreshLayout refresh;
    private LinearLayout errorPage;

    SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            mainWeb.reload();
        }
    };
    private int oritation;
    private int mScrollY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        webConfigSetting();
    }

    private void initView() {
        mainWeb = (FoundWebView) findViewById(R.id.main_web);
        refresh = (SwipeRefreshLayout) findViewById(R.id.refresh);
        refresh.setOnRefreshListener(listener);

        errorPage = (LinearLayout) findViewById(R.id.error_page);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void webConfigSetting() {
        mainWeb.setWebViewClient(mWebViewClient);
        webSettings = mainWeb.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setTextZoom(100);
        mainWeb.loadUrl(EDU_PLATFORM_URL);
        mainWeb.setOnCustomScroolChangeListener(new FoundWebView.ScrollInterface() {

            @Override
            public void onSChanged(int l, int t, int oldl, int oldt) {

                if (mainWeb.getScrollY() == 0) {
                    String currentUrl = mainWeb.getUrl();
                    if (currentUrl.contains("learn#lesson")) {
                        refresh.setEnabled(false);
                    } else {
                        refresh.setEnabled(true);
                    }

                } else {
                    refresh.setEnabled(false);
                }
            }
        });

    }




    private WebViewClient mWebViewClient = new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        };


        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            refresh.setRefreshing(false);
        }

        public void onReceivedError(final WebView view, int errorCode,
                                    String description, final String failingUrl) {

            String data = "Page NO FOUND！";
            mainWeb.loadUrl("javascript:document.body.innerHTML=\"" + data
                    + "\"");
            mainWeb.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mainWeb.loadUrl(EDU_PLATFORM_URL);

                }
            });
        }

    };



    private View mView = null;


    private long currentTime;
    private String currentUrl;



    private void pressTwiceToBack() {
        if (System.currentTimeMillis() - currentTime > 2000) {
            Toast.makeText(MainActivity.this, "再次点击退出系统",
                    Toast.LENGTH_SHORT).show();
            currentTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }
    public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
        currentUrl = mainWeb.getUrl();
        Log.e("currentUrl", currentUrl);
        Log.e("isTrue",
                currentUrl.contains(EDU_PLATFORM_URL + "course/explore") + "");
        Log.e("course", EDU_PLATFORM_URL + "/course/explore");
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (currentUrl.equals(EDU_PLATFORM_URL)) {
                pressTwiceToBack();
                return true;
            } else {
                if (mainWeb.canGoBack()) {
                    mainWeb.goBack();
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }


}
